#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QGroupBox>
#include <QScrollArea>
#include <QMessageBox>
#include <QStatusBar>
#include <QFrame>
#include <QTimer>
#include <QThread>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QFont>
#include <QQueue>
#include <QMutex>
#include <QWaitCondition>

#include "windowcapture.h"
#include "websocketserver.h"
#include "audiocapture.h"
#include "application_manager.h"

class ImageDisplayWidget : public QWidget
{
    Q_OBJECT

public:
    explicit ImageDisplayWidget(QWidget *parent = nullptr);
    void displayImage(const QImage &image, const QString &qualitySetting = "平衡");

private:
    QLabel *m_imageLabel;
    QScrollArea *m_scrollArea;
    QPixmap m_currentPixmap;
};

class ContinuousCaptureThread : public QThread
{
    Q_OBJECT

public:
    explicit ContinuousCaptureThread(const QString &windowTitle, int fps, bool isFullscreen, bool useWinRT = false, QObject *parent = nullptr);
    ~ContinuousCaptureThread();

    void stopCapture();
    void updateTarget(const QString &windowTitle, bool isFullscreen);
    void updateFps(int fps);
    void updateCaptureMethod(bool useWinRT);
    void setCaptureItem(winrt::Windows::Graphics::Capture::GraphicsCaptureItem item);

signals:
    void frameCaptured(const QImage &image);
    void fpsUpdated(float fps);

protected:
    void run() override;

private:
    void captureLoop();
    void outputLoop();

    QString m_windowTitle;
    int m_fps;
    bool m_isFullscreen;
    bool m_isRunning;
    bool m_useWinRT;
    
    // WinRT捕获项（在主线程中创建）
    winrt::Windows::Graphics::Capture::GraphicsCaptureItem m_captureItem;
    
    // 移除队列机制，直接发送信号
    
    // 帧率控制
    qint64 m_targetFrameInterval; // 微秒
    qint64 m_nextOutputTime;
    
    // 统计信息
    int m_frameCount;
    qint64 m_lastFpsTime;
    QList<qint64> m_captureTimes;
    static const int MAX_CAPTURE_TIMES = 30;
    
    // 性能统计
    int m_capturedFrames;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void onWindowSelectionChanged(const QString &windowTitle);
    void onQualityChanged(const QString &quality);
    void onFpsChanged(int fps);
    void onFrameCaptured(const QImage &image);
    void onFpsUpdated(float fps);
    void onWebSocketStatusUpdated();
    void onCaptureMethodChanged();
    void refreshWindows();
    void toggleCapture();

private:
    void setupUI();
    void setupStatusBar();
    void startContinuousCapture(const QString &windowTitle = QString(), bool isFullscreen = false);
    void stopContinuousCapture();
    void displayImage(const QImage &image);
    void updateResolutionDisplay(const QImage &image);
    
    // 帧队列处理
    void processFrameQueue();
    void addFrameToQueue(const QImage &image);
    void updateWebSocketStatus();

    // UI组件
    QWidget *m_centralWidget;
    QVBoxLayout *m_mainLayout;
    
    // 窗口选择区域
    QGroupBox *m_windowGroup;
    QComboBox *m_captureMethodCombo;
    QComboBox *m_windowCombo;
    QPushButton *m_refreshBtn;
    
    // 捕获控制区域
    QGroupBox *m_controlGroup;
    QSpinBox *m_fpsSpinBox;
    QComboBox *m_qualityCombo;
    QLabel *m_websocketStatus;
    QPushButton *m_startStopBtn;
    
    // 图像显示区域
    QGroupBox *m_imageGroup;
    ImageDisplayWidget *m_imageDisplay;
    
    // 状态栏
    QLabel *m_resolutionLabel;
    QLabel *m_statusLabel;
    QLabel *m_websocketInfo;
    
    // 核心功能
    WebSocketServer *m_websocketServer;
    AudioCapture *m_audioCapture;
    ApplicationManager *m_applicationManager;
    
    // 捕获方式
    bool m_useWinRT;
    
    // 连续捕获
    ContinuousCaptureThread *m_continuousCaptureThread;
    bool m_isCapturing;
    
    // 定时器
    QTimer *m_websocketTimer;
    
    // 状态
    QImage m_capturedImage;
    bool m_websocketEnabled;
    
    // 帧缓存队列
    QQueue<QImage> m_frameQueue;
    QMutex m_frameQueueMutex;
    static const int MAX_FRAME_QUEUE_SIZE = 60;
    QTimer *m_frameProcessTimer;
};

#endif // MAINWINDOW_H
